package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Origin;
import com.aaa.vegetablemarketexpress.service.OriginService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
 * @FileName: OriginController
 * @Description: 商品产地控制器
 * @Author: qxh
 * @CreateTime: 2025/9/8 15:39
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/origin")
@Log4j2
public class OriginController {

    @Resource
    private OriginService originService;

    /**
     * 获取产地列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody Origin origin) {
        log.info("开始查询产地列表");
        List<Origin> list = originService.selectOriginList(origin);
        log.info("查询产地列表结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 分页获取产地列表
     */
    @PostMapping("/listByPage")
    public Result listByPage(@RequestBody Page<Origin> page) {
        log.info("开始执行产地分页查询");
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();

        if (pageNo == null || pageSize == null || pageNo <= 0 || pageSize <= 0) {
            return ResultUtil.failed("分页参数不正确");
        }

        com.github.pagehelper.PageHelper.startPage(pageNo, pageSize);
        List<Origin> list = originService.selectOriginList(page.getData());
        PageInfo<Origin> pageInfo = new PageInfo<>(list);
        log.info("产地分页查询结束，共查询到{}条记录", pageInfo.getTotal());
        return ResultUtil.success(pageInfo);
    }

    /**
     * 获取所有正常状态的产地（用于下拉选择）
     */
    @GetMapping("/listAllNormal")
    public Result listAllNormal() {
        log.info("开始查询所有正常状态的产地");
        List<Origin> list = originService.selectAllNormalOrigin();
        log.info("查询所有正常状态的产地结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 统计产地数量
     */
    @PostMapping("/count")
    public Result count(@RequestBody Origin origin) {
        log.info("开始统计产地数量");
        int count = originService.countOrigin(origin);
        log.info("统计产地数量结束，统计结果为{}", count);
        return ResultUtil.success(count);
    }

    /**
     * 获取产地详细信息
     */
    @GetMapping("/{originId}")
    public Result getInfo(@PathVariable("originId") Long originId) {
        log.info("开始查询产地详细信息，产地ID: {}", originId);
        Origin origin = originService.selectOriginById(originId);
        if (origin == null) {
            log.warn("未找到ID为{}的产地信息", originId);
            return ResultUtil.failed("未找到对应的产地信息");
        }
        log.info("查询产地详细信息结束");
        return ResultUtil.success(origin);
    }

    /**
     * 新增产地
     */
    @PostMapping
    public Result add(@RequestBody Origin origin) {
        log.info("开始新增产地，产地名称: {}", origin.getOriginName());
        try {
            int result = originService.insertOrigin(origin);
            if (result > 0) {
                log.info("新增产地成功，产地ID: {}", origin.getOriginId());
                return ResultUtil.success("新增产地成功");
            } else {
                log.warn("新增产地失败");
                return ResultUtil.failed("新增产地失败");
            }
        } catch (Exception e) {
            log.error("新增产地时发生异常", e);
            return ResultUtil.failed("新增产地失败: " + e.getMessage());
        }
    }

    /**
     * 修改产地
     */
    @PutMapping
    public Result edit(@RequestBody Origin origin) {
        log.info("开始修改产地，产地ID: {}", origin.getOriginId());
        if (origin.getOriginId() == null) {
            return ResultUtil.failed("产地ID不能为空");
        }
        try {
            int result = originService.updateOrigin(origin);
            if (result > 0) {
                log.info("修改产地成功，产地ID: {}", origin.getOriginId());
                return ResultUtil.success("修改产地成功");
            } else {
                log.warn("修改产地失败");
                return ResultUtil.failed("修改产地失败");
            }
        } catch (Exception e) {
            log.error("修改产地时发生异常", e);
            return ResultUtil.failed("修改产地失败: " + e.getMessage());
        }
    }

    /**
     * 删除产地
     */
    @DeleteMapping("/{originId}")
    public Result remove(@PathVariable Long originId) {
        log.info("开始删除产地，产地ID: {}", originId);
        if (originId == null) {
            return ResultUtil.failed("产地ID不能为空");
        }
        try {
            int result = originService.deleteOriginById(originId);
            if (result > 0) {
                log.info("删除产地成功，产地ID: {}", originId);
                return ResultUtil.success("删除产地成功");
            } else {
                log.warn("删除产地失败");
                return ResultUtil.failed("删除产地失败");
            }
        } catch (Exception e) {
            log.error("删除产地时发生异常", e);
            return ResultUtil.failed("删除产地失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除产地
     */
    @DeleteMapping("/batch/{originIds}")
    public Result removeBatch(@PathVariable Long[] originIds) {
        log.info("开始批量删除产地，产地IDs: {}", (Object) originIds);
        if (originIds == null || originIds.length == 0) {
            return ResultUtil.failed("请选择要删除的产地");
        }
        try {
            int result = originService.deleteOriginByIds(originIds);
            if (result > 0) {
                log.info("批量删除产地成功，删除数量: {}", result);
                return ResultUtil.success("批量删除产地成功");
            } else {
                log.warn("批量删除产地失败");
                return ResultUtil.failed("批量删除产地失败");
            }
        } catch (Exception e) {
            log.error("批量删除产地时发生异常", e);
            return ResultUtil.failed("批量删除产地失败: " + e.getMessage());
        }
    }

    /**
     * 校验产地名称是否唯一
     */
    @GetMapping("/checkOriginNameUnique")
    public Result checkOriginNameUnique(@RequestBody Origin origin) {
        log.info("开始校验产地名称唯一性，产地名称: {}", origin.getOriginName());
        String result = originService.checkOriginNameUnique(origin);
        log.info("校验产地名称唯一性结束，结果: {}", result);
        return ResultUtil.success(result);
    }

    /**
     * 更新产地状态
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody Origin origin) {
        log.info("开始更新产地状态，产地ID: {}, 状态: {}", origin.getOriginId(), origin.getStatus());
        if (origin.getOriginId() == null) {
            return ResultUtil.failed("产地ID不能为空");
        }
        try {
            int result = originService.updateOriginStatus(origin.getOriginId(), String.valueOf(origin.getStatus()));
            if (result > 0) {
                log.info("更新产地状态成功，产地ID: {}", origin.getOriginId());
                return ResultUtil.success("更新产地状态成功");
            } else {
                log.warn("更新产地状态失败");
                return ResultUtil.failed("更新产地状态失败");
            }
        } catch (Exception e) {
            log.error("更新产地状态时发生异常", e);
            return ResultUtil.failed("更新产地状态失败: " + e.getMessage());
        }
    }
}
